package com.yyge.web.servlet;

import cn.hutool.crypto.digest.BCrypt;
import com.yyge.dao.Impl.UserInfoDaoImpl;
import com.yyge.entity.BaseEntity;
import com.yyge.entity.Userinfo;
import com.yyge.util.JwtUtile;
import com.yyge.util.ServletUtil;
import com.yyge.vo.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 用户修改的API
 * 参数：参考
 * {
    "name": "admin",
    "password":"123456789",
    "repassword":"123456789",
    "age":18,
    "sex":0,
    "phone":9999999999
}
 */

@WebServlet("/updata/")
public class UserinfoUpdata extends HttpServlet {

    @Override
    protected  void  doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BaseEntity baseEntity = ServletUtil.fetchDataFromRequest(req,Userinfo.class);
        Map<String, Object> params = baseEntity.getParams();
        Userinfo targrt = (Userinfo) baseEntity;
        UserInfoDaoImpl impl=new UserInfoDaoImpl();

        //传入参数完整性
         R r=new R();
        if(targrt.getId() == null){
            r=R.fail("错误的传参!失去主键");
            ServletUtil.writeResult2Client(resp,r);
            return;
        }
        //修改之前验证Token
        Userinfo updataUser = impl.findbyId(targrt.getId());
        if(!JwtUtile.tokenVerify(updataUser.getToken())){
            r=R.fail("错误！您无权操作!");
            ServletUtil.writeResult2Client(resp,r);
        }

        if(updataUser.getName() == null){
            r=R.fail("修改失败!未找到该用户");
            ServletUtil.writeResult2Client(resp,r);
            return;
        }

        //如果不等于名称则赋值
        if(targrt.getName()!=null && targrt.getName() !=updataUser.getName()){
            updataUser.setName(targrt.getName());
        }
        //如果不等于电话则赋值
        if(targrt.getPhone()!=null && targrt.getPhone() !=updataUser.getPhone()){
            updataUser.setPhone(targrt.getPhone());
        }
        //如果不等于头像则赋值
        if(targrt.getAvatar()!=null && targrt.getAvatar() !=updataUser.getAvatar()){
            updataUser.setName(targrt.getAvatar());
        }
        //如果不等于年龄则赋值
        if(targrt.getAge()!=null && targrt.getAge() !=updataUser.getAge()){
            updataUser.setAge(targrt.getAge());
        }

        //如果不等于性别则赋值
        if(targrt.getSex()!=null && targrt.getSex() !=updataUser.getSex()){
            updataUser.setSex(targrt.getSex());
        }

        //校验密码 修改
        if(targrt.getParams()!=null){
            if(!BCrypt.checkpw(targrt.getPassword(),updataUser.getPassword())){
                String pass=BCrypt.hashpw(targrt.getPassword());
                updataUser.setPassword(pass);
            }
        }
        int modify = impl.modify(updataUser);
        if(modify>0){
            r=R.ok("修改成功！");
            ServletUtil.writeResult2Client(resp,r);

        }else{
            r=R.fail("修改异常！");
            ServletUtil.writeResult2Client(resp,r);
        }


    }
}
